local skel = fk.CreateSkill {
  name = "rmt__changdao",
}

Fk:loadTranslationTable{
  ["rmt__changdao"] = "畅道",
  [":rmt__changdao"] = "准备阶段，你可摸二至四张牌，跳过回合内等量个连续的阶段（准备阶段和结束阶段除外）；结束阶段，若当前回合你跳过摸牌阶段，可移动场上一张牌，未跳过弃牌阶段，可回复一点体力。",

  ["#rmt__changdao-first"] = "畅道：选择你要跳过的第一个阶段",
  ["#rmt__changdao-last"] = "畅道：选择你要跳过的最后一个阶段（跳过与第一次选择之间的阶段）",
  ["changdao__move"] = "移动场上一张牌",
  ["changdao__recover"] = "回复1点体力",
  ["#rmt__changdao-move"] = "畅道：请选择两名角色，移动其中一名角色场上的一张牌给另一名角色。",
  ["#rmt__changdao-choice"] = "畅道：选择你要执行的项",
  ["changdao_choice"] = "%arg：%arg2",
}

---@param p PhaseData
local phaseCheck = function (p) return p.phase ~= Player.Start and p.phase ~= Player.Finish and not p.skipped end

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(skel.name)) then return false end
    local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn)
    if turn_event == nil then return false end
    local turn_data = turn_event.data
    if player.phase == Player.Start then
      local phaselist = {}
      for i = turn_data.phase_index + 1, #turn_data.phase_table do
        local phase = turn_data.phase_table[i]
        if phaseCheck(phase) then
          if (turn_data.phase_table[i-1] and phaseCheck(turn_data.phase_table[i-1]))
          or (turn_data.phase_table[i+1] and phaseCheck(turn_data.phase_table[i+1])) then
            table.insert(phaselist, {i, Util.PhaseStrMapper(phase.phase)})
          end
        end
      end
      if #phaselist >= 2 then
        event:setCostData(self, phaselist)
        return true
      end
    elseif target.phase == Player.Finish then
      local cost_data = {}
      local skipped = {}
      for _, p in ipairs(turn_data.phase_table) do
        if p.skipped then
          table.insert(skipped, p.phase)
        end
      end
      if table.contains(skipped, Player.Draw) and #player.room:canMoveCardInBoard() > 0 then
        table.insert(cost_data, "changdao__move")
      end
      if not table.contains(skipped, Player.Discard) and player:isWounded() then
        table.insert(cost_data, "changdao__recover")
      end
      if #cost_data > 0 then
        event:setCostData(self, cost_data)
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cost_data = event:getCostData(self)
    if target.phase == Player.Start then
      -- 如果我主要阶段不止4个，那得这么处理
      local turn_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn)
      if turn_event == nil then return false end
      local turn_data = turn_event.data
      local phaselist, indexs, choices = {}, {}, {}
      for _, dat in ipairs(cost_data) do
        table.insert(phaselist, dat[2])
        table.insert(indexs, dat[1])
        table.insert(choices, "changdao_choice:::"..dat[1]..":"..dat[2])
      end
      local first = room:askToChoice(player, {choices = choices, skill_name = skel.name, prompt = "#rmt__changdao-first"})
      local all_choices = table.simpleClone(choices)
      local splitter = first:split(":")
      local first_phase_index = tonumber(splitter[4]) -- 第一个选择的阶段在回合表中的序号
      choices = {}
      for i = 1, 3 do -- 沿着首个序号往下找3个
        local p = turn_data.phase_table[first_phase_index + i]
        if p and phaseCheck(p) then
          table.insert(choices, "changdao_choice:::".. (first_phase_index + i) .. ":" .. Util.PhaseStrMapper(p.phase))
        else
          break
        end
      end
      if #choices == 0 then return end
      local last = room:askToChoice(player, {choices = choices, skill_name = skel.name,
      prompt = "#rmt__changdao-last", all_choices = all_choices})
      local num = table.indexOf(choices, last) + 1
      for i = first_phase_index, first_phase_index + num - 1 do
        if turn_data.phase_table[i] then
          turn_data.phase_table[i].skipped = true
        end
      end
      player:drawCards(num, skel.name)
    elseif target.phase == Player.Finish then
      local chosen = room:askToChoices(player, {
        choices = cost_data, min_num = 1, max_num = #cost_data, cancelable = false, skill_name = skel.name,
        prompt = "#rmt__changdao-choice",
      })
      if table.contains(chosen, "changdao__move") and not player.dead then
        local targets = room:askToChooseToMoveCardInBoard(player, {
          prompt = "#rmt__changdao-move", skill_name = skel.name, cancelable = false
        })
        if #targets ~= 0 then
          room:askToMoveCardInBoard(player, {target_one = targets[1], target_two = targets[2], skill_name = skel.name })
        end
      end
      if table.contains(chosen, "changdao__recover") and not player.dead then
        room:recover({
          who = player,
          num = 1,
          recoverBy = player,
          skillName = skel.name
        })
      end
    end
  end,
})



return skel
